﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Runtime = Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using System.Data.SqlClient;
using SSISFromCode.Helpers;

namespace SSISFromCode
{

    internal abstract class NonFlatFileGenerator : PackageGenerator, IRequireTransform
    {
        private IDTSComponentMetaData100 _dataConversion;
        private CManagedComponentWrapper _dataConversionInstance;

        protected override void CreateDataFlowComponents( Runtime.Application app )
        {
            base.CreateDataFlowComponents( app );
            CreateDataFlowConversionComponent( app );
        }

        private void CreateDataFlowConversionComponent( Runtime.Application app )
        {
            _dataConversion = _pipe.ComponentMetaDataCollection.New();
            _dataConversion.Name = COMPONENT_NAMES.DATA_CONVERSION;
            _dataConversion.ComponentClassID = app.PipelineComponentInfos[OBJECT_CREATION.DATA_CONVERSION].CreationName;
        }

        protected override void CreateDesignInstances()
        {
            base.CreateDesignInstances();

            _dataConversionInstance = _dataConversion.Instantiate();
            _dataConversionInstance.ProvideComponentProperties();
        }

        protected override void CreateTable( SqlConnection openConnection )
        {
            var destinationColumns = DestinationColumns();
            var columnWidth = TransformationGeneratorHelper.ColumnWidth( this, destinationColumns.Count );
            if ( columnWidth < MISC.COL_WIDTH_LOW_THRESHOLD )
            {
                Messages.Add( IMPORT_MESSAGES.COL_WIDTH_LOW_THRESHOLD, this.GetType().ToString(), Convert.ToString(MISC.COL_WIDTH_LOW_THRESHOLD) );
            }
            base.CreateTable( destinationColumns, openConnection, columnWidth );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// <remarks>PackageGenerator.ReinitializeMetadata() must have been called for the source component.</remarks>
        private List<string> DestinationColumns()
        {
            var componentColumns = _dataSource.OutputCollection[0].OutputColumnCollection;
            var simpleColumns = new List<string>( componentColumns.Count );
            foreach ( IDTSOutputColumn100 column in componentColumns )
            {
                simpleColumns.Add( column.Name );
            }
            return simpleColumns;
        }

        protected override void ConnectDataFlowSubComponents()
        {
            TransformationGeneratorHelper.ConnectDataFlowSubComponents( _pipe, _dataSource, _dataConversion, _dataDest );
        }

        /// <summary>
        /// Map columns from the data conversion transform to the destination component
        /// </summary>
        protected override void DoActualMapping()
        {
            TransformationGeneratorHelper.MapTransformedColumns( _dataDest, _dataDestInstance );
        }

        void IRequireTransform.ConfigureDataTransform()
        {
            TransformationGeneratorHelper.ConfigureDataTransformation( _dataConversion, _dataConversionInstance, this );
        }

    }

}
